package com.biglead.mini.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biglead.mini.mapper.*;
import com.biglead.mini.pojo.*;
import com.biglead.mini.result.PmsPortalProductDetail;
import com.biglead.mini.service.PmsPortalProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PmsPortalProductServiceImpl extends ServiceImpl<PmsProductMapper, PmsProduct> implements PmsPortalProductService {

    @Autowired
    private PmsProductCategoryMapper pmsProductCategoryMapper;

    @Autowired
    private PmsProductAttributeMapper pmsProductAttributeMapper;

    @Autowired
    private PmsProductAttributeValueMapper pmsProductAttributeValueMapper;
    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    @Autowired
    private PmsSkuStockMapper pmsSkuStockMapper;

    @Autowired
    private PmsProductLadderMapper pmsProductLadderMapper;

    @Autowired
    private PmsProductFullReductionMapper pmsProductFullReductionMapper;

    @Autowired
    private PmsProductMapper pmsProductMapper;

    @Override
    public Page<PmsProduct> search(String keyword, Long brandId, Long productCategoryId, Integer pageNum, Integer pageSize, Integer sort) {
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.isNotEmpty(keyword), PmsProduct::getName, keyword);

        queryWrapper.eq(brandId != null, PmsProduct::getBrandId, brandId);
        queryWrapper.eq(productCategoryId != null, PmsProduct::getProductCategoryId, productCategoryId);
        //1->按新品；2->按销量；3->价格从低到高；4->价格从高到低
        if (sort == 1) {
            queryWrapper.orderByDesc(PmsProduct::getId);
        } else if (sort == 2) {
            queryWrapper.orderByDesc(PmsProduct::getSale);
        } else if (sort == 3) {
            queryWrapper.orderByAsc(PmsProduct::getPrice);
        } else if (sort == 4) {
            queryWrapper.orderByDesc(PmsProduct::getPrice);
        }
        //未删除
        queryWrapper.eq(PmsProduct::getDeleteStatus, 0);
        //已上架
        queryWrapper.eq(PmsProduct::getPublishStatus, 1);

        return this.page(Page.of(pageNum, pageSize), queryWrapper);
    }

    @Override
    public List<PmsProductCategoryNode> categoryTreeList() {
        List<PmsProductCategory> allList = pmsProductCategoryMapper.selectList(new LambdaQueryWrapper<PmsProductCategory>());
        List<PmsProductCategoryNode> result = allList.stream()
                .filter(item -> item.getParentId().equals(0L))
                .map(item -> covert(item, allList)).collect(Collectors.toList());
        return result;
    }

    @Override
    public PmsPortalProductDetail detail(Long id) {
        PmsPortalProductDetail result = new PmsPortalProductDetail();
        //获取商品信息
        PmsProduct product = this.getById(id);
        result.setProduct(product);
        //获取品牌信息
        PmsBrand brand = pmsBrandMapper.selectById(product.getBrandId());
        result.setBrand(brand);

        //获取商品属性信息
        List<PmsProductAttribute> productAttributeList = pmsProductAttributeMapper.selectList(new LambdaQueryWrapper<PmsProductAttribute>().eq(PmsProductAttribute::getProductAttributeCategoryId, product.getProductAttributeCategoryId()));
        result.setProductAttributeList(productAttributeList);
        //获取商品属性值信息
        if (!productAttributeList.isEmpty()) {
            List<Long> attributeIds = productAttributeList.stream().map(PmsProductAttribute::getId).collect(Collectors.toList());
            List<PmsProductAttributeValue> productAttributeValueList = pmsProductAttributeValueMapper.selectList(
                    new LambdaQueryWrapper<PmsProductAttributeValue>().
                            eq(PmsProductAttributeValue::getProductId, product.getId())
                            .in(PmsProductAttributeValue::getProductAttributeId, attributeIds)
            );
            result.setProductAttributeValueList(productAttributeValueList);
        }

        //获取商品SKU库存信息
        List<PmsSkuStock> skuStockList = pmsSkuStockMapper.selectList(new LambdaQueryWrapper<PmsSkuStock>().eq(PmsSkuStock::getProductId,product.getId()));
        result.setSkuStockList(skuStockList);

        //商品阶梯价格设置
        if(product.getPromotionType()==3){
            List<PmsProductLadder> productLadderList = pmsProductLadderMapper.selectList(new LambdaQueryWrapper<PmsProductLadder>().eq(PmsProductLadder::getProductId,product.getId()));
            result.setProductLadderList(productLadderList);
        }

        //商品满减价格设置
        if(product.getPromotionType()==4){
            List<PmsProductFullReduction> productFullReductionList = pmsProductFullReductionMapper.selectList(new LambdaQueryWrapper<PmsProductFullReduction>().eq(PmsProductFullReduction::getProductId,product.getId()));
            result.setProductFullReductionList(productFullReductionList);
        }

        //商品可用优惠券
        result.setCouponList(pmsProductMapper.getAvailableCouponList(product.getId(),product.getProductCategoryId()));

        return result;
    }

    /**
     * 初始对象转化为节点对象
     */
    private PmsProductCategoryNode covert(PmsProductCategory item, List<PmsProductCategory> allList) {
        PmsProductCategoryNode node = new PmsProductCategoryNode();
        BeanUtils.copyProperties(item, node);
        List<PmsProductCategoryNode> children = allList.stream()
                .filter(subItem -> subItem.getParentId().equals(item.getId()))
                .map(subItem -> covert(subItem, allList)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }
}
